package com.baizan.daka.ui.presenter

import com.baizan.daka.base.BasePresenter
import com.baizan.daka.mvp.model.UploadModel
import com.baizan.daka.ui.contract.UserInfoContract
import com.baizan.daka.mvp.model.UserModel
import com.baizan.daka.mvp.model.bean.UserInfoBean
import com.baizan.daka.net.bean.ModifyUserBean
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast
import java.io.File

class UserInfoPresenter(val mView: UserInfoContract.View) : BasePresenter(),
    UserInfoContract.Presenter {
    private val userModel: UserModel by lazy {
        UserModel()
    }
    private val uploadModel: UploadModel by lazy {
        UploadModel()
    }
    override fun upload(file: File) {
        uploadModel.upload(file)
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {

                override fun onCallNext(t: String?) {
                    mView.uploadSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

    override fun submitInfo(userName: String, userIcon: String?, sex: Int) {
        if (userName.isNullOrEmpty()) {
            showToast("请输入用户名")
            return
        }
        userModel.modify(ModifyUserBean(sex, userIcon, userName))
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {

                override fun onCallNext(t: String?) {
                    mView.modifySuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }
    override fun getUserInfo() {
        mView.showLoadingDialog()
        userModel.getUserInfo()
            .subscribe(object : CommonSubscriber<UserInfoBean>(compositeDisposable) {

                override fun onCallNext(t: UserInfoBean?) {
                    mView.loadSuccess(t!!)
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }
            })
    }

}

